package com.gluck.cases.app.biz.impl.excel;

import com.gluck.cases.core.modal.dto.TestCaseDTO;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName AbstractExcelHandler
 * @Desc excel 责任链
 * @Author shiyongzhe
 * @Date 2022/5/24 - 5:58 下午
 **/
public abstract class AbstractExcelHandler<T> {

    protected AbstractExcelHandler nextHandler;

    protected void next(AbstractExcelHandler handler) {
        this.nextHandler = handler;
    }

    public abstract void doHandler(T data, Builder<T> excelBuilder) throws IOException;

    // Builder静态类
    public static class Builder<T> {

        private AbstractExcelHandler<T> head;
        private AbstractExcelHandler<T> tail;
        private List<TestCaseDTO> cases;
        private String sheetname;
        private String wbname;
        private Workbook wb;
        // 需要反射的实体
        private Class<?> invokeEntity;
        private List<TestCaseDTO> caseEntities;

        public Builder<T> addHandler(AbstractExcelHandler<T> handler) {
            if (this.head == null) {
                this.head = this.tail = handler;
                return this;
            }
            this.tail.next(handler);
            this.tail = handler;
            return this;
        }

        public AbstractExcelHandler<T> build() {
            // 添加默认的流程结尾
            AbstractExcelHandler baseHandler = new BaseHandler();
            if (this.tail != null) {
                this.tail.next(baseHandler);
                this.tail = baseHandler;
            }
            if (this.head == null) {
                this.head = baseHandler;
            }
            return this.head;
        }

        public Builder setCases(List<TestCaseDTO> cases) {
            this.cases = cases;
            return this;
        }

        public Builder setSheetname(String sheetname) {
            this.sheetname = sheetname;
            return this;
        }

        public Builder setWbname(String wbname) {
            this.wbname = wbname;
            return this;
        }

        public Workbook getWb() {
            return wb;
        }

        public Builder setWb(Workbook wb) {
            this.wb = wb;
            return this;
        }

        public List<TestCaseDTO> getCases() {
            return cases;
        }

        public String getSheetname() {
            return sheetname;
        }

        public String getWbname() {
            return wbname;
        }

        public Class<?> getInvokeEntity() {
            return invokeEntity;
        }

        public void setInvokeEntity(Class<?> invokeEntity) {
            this.invokeEntity = invokeEntity;
        }

        public List<TestCaseDTO> getCaseEntities() {
            return caseEntities;
        }

        public void setCaseEntities(List<TestCaseDTO> caseEntities) {
            this.caseEntities = caseEntities;
        }
    }
}
